<?php

if( !defined("IN_ELCIUS") ) exit;


// =======================================================|
// removes all except the characters provided
// Example: Clean( "4n-3v1L_57r1nG!", "a-z" );
//			Clean( "4n-3v1L_57r1nG!", "0-9\!\_\-" );
// Returns: "nvrn"
//			"4-31_571!"
function Clean( $data, $allowed = "a-zA-Z0-9" )
{

	if( empty( $data ) )
	{ // empty string/array
		return $data;
	}

	if( is_array( $data ) )
	{ // array of strings

		foreach( $data as $index => $value )
		{ // loop through the array
			$data[ $index ] = clean( $value, $allowed );
		}

		return $data;

	}
	else
	{ // single string

		@preg_match_all( "/([${allowed}]+)/e", $data, $goods );
		if( !is_array( $goods ) )
		{
			return 0;
		}
		$goods = implode( "", $goods[0] );
		return $goods;

	}

	return false;

}

// =======================================================|
// cuts a string from start to end chars
// Example: substrat( "foo<bar>boo", "<", ">" );
//			substrat( "foo<bar>boo", ">", true );
//			substrat( "foo<bar>boo", "<", false );
// Returns: "bar" 	- str1 to str2
//			"boo" 	- str1 to end of string
//			"foo" 	- start of string to str1
function substrat( $str, $fStr1, $fStr2 = false )
{

	if( !strlen( $fStr1 ) or strstr( $str, $fStr1 ) === false )
	{ // first string not set/found
		return $str;
	}
	else
	{ // first string was found

		if( strlen( $fStr2 ) and strstr( $str, $fStr2 ) == true )
		{ // from string 1 to string 2
			$loc1 = strpos( $str, $fStr1 ) + strlen( $fStr1 );
			$loc2 = strpos( substr( $str, $loc1 ), $fStr2 );
		}
		else
		{ // second string not set/found

			if( $fStr2 === true )
			{ // from string 1 to end
				$loc1 = strpos( $str, $fStr1 ) + strlen( $fStr1 );
				$loc2 = strlen( $str );
			}
			else
			{ // from start to string 1
				$loc1 = 0;
				$loc2 = strpos( $str, $fStr1 );
			}

		}

		$str = substr( $str, $loc1, $loc2 );

		if( !$str )
		{ // substr returned bool false
			$str = "";
		}

		return $str;

	}

}

// =======================================================|
// replaces familiar html tags with bb code
function html2bb( $string, $remove_other = false )
{
	$find = array
	(

		"'<br(.*?)>'i",

		"'<strong(.*?)>(.*?)</strong>'is",
		"'<b(.*?)>(.*?)</b>'is",
		"'<(.*?)style=(.*?)font-weight:([\s]+|)bold([\;]|[\s]+|)(.*?)>(.*?)</(.*?)>'is",

		"'<em(.*?)>(.*?)</em>'i",
		"'<i(.*?)>(.*?)</i>'i",
		"'<(.*?)style=(.*?)font-style:([\s]+|)italic([\;]|[\s]+|)(.*?)>(.*?)</(.*?)>'is",

		"'<u(.*?)>(.*?)</u>'i",
		"'<(.*?)style=(.*?)text-decoration:([a-z\s\-]+)underline([\;]|[\s]+|)(.*?)>(.*?)</(.*?)>'is",
		"'<(.*?)style=(.*?)text-decoration:([a-z\s\-]+)overline([\;]|[\s]+|)(.*?)>(.*?)</(.*?)>'is",
		"'<strike(.*?)>(.*?)</strike>'i",
		"'<(.*?)style=(.*?)text-decoration:([a-z\s\-]+)line-through([\;]|[\s]+|)(.*?)>(.*?)</(.*?)>'is",

		"'<(.*?)style=(.*?)background-color:([\s]+|)(#[0-F]+|[a-z]+)([\;]|[\s]+|)(.*?)>(.*?)</(.*?)>'is",
		"'<(.*?)style=(.*?)color:([\s]+|)(#[0-F]+|[a-z]+)([\;]|[\s]+|)(.*?)>(.*?)</(.*?)>'is",

		"'<(.*?)style=(.*?)text-align:([\s]+|)center([\;]|[\s]+|)(.*?)>(.*?)</(.*?)>'is",
		"'<(.*?)style=(.*?)text-align:([\s]+|)(left|right)([\;]|[\s]+|)(.*?)>(.*?)</(.*?)>'is",

		"'<ul(.*?)>'i",
		"'</ul>'i",
		"'<ol(.*?)>'i",
		"'</ol>'i",
		"'<li(.*?)>(.*?)</li>'is",

		"'<(.*?)style=(.*?)margin-left:([\s]+|)([0-9]+)(px|\%)([\;]|[\s]+|)(.*?)>(.*?)</(.*?)>'is",

		"'<sub(.*?)>(.*?)</sub>'is",
		"'<sup(.*?)>(.*?)</sup>'is",

		"'<img(.*?)src=([\"\'\s])TODO\/(.*?)([\"\'\s])(.*?)>'i",
		"'<img(.*?)src=([\"\'\s])(.*?)([\"\'\s])(.*?)>'i",

	);

	$replace = array
	(

		"[br]",

		"[b]\\2[/b]",
		"[b]\\2[/b]",
		"<\\1style=\\2\\5>[b]\\6[/b]</\\7>",

		"[i]\\2[/i]",
		"[i]\\2[/i]",
		"<\\1style=\\2\\5>[i]\\6[/i]</\\7>",

		"[u]\\2[/u]",
		"<\\1style=\\2text-decoration:\\3\\5>[u]\\6[/u]</\\7>",
		"<\\1style=\\2text-decoration:\\3\\5>[o]\\6[/o]</\\7>",
		"[s]\\2[/s]",
		"<\\1style=\\2text-decoration:\\3\\5>[s]\\6[/s]</\\7>",

		"<\\1style=\\2\\6>[bgcolor=\\4]\\7[/bgcolor]</\\8>",
		"<\\1style=\\2\\6>[color=\\4]\\7[/color]</\\8>",

		"<\\1style=\\2\\5>[center]\\6[/center]</\\7>",
		"<\\1style=\\2\\6>[align=\\4]\\7[/align]</\\8>",

		"[ul]",
		"[/ul]",
		"[ol]",
		"[/ol]",
		"[li]\\2[/li]",

		"<\\1style=\\2\\7>[indent=\\4\\5]\\8[/indent]</\\9>",

		"[sub]\\2[/sub]",
		"[sup]\\2[/sup]",

		"[image=TODO/\\3]",
		"[image=TODO/\\3]",

	);

	if( $remove_other == true )
	{ // removes any unknown/unreplaced html tags from string
		$find[] = "'<(.*?)>'";
		$replace[] = "";
	}

	return preg_replace( $find, $replace, $string );

}

// =======================================================|
// replaces bb code with html tags
function bb2html( $string, $simple = true )
{

	$find = array_slice( array(
		"'\[b\](.*?)\[/b\]'is",

		"'\[i\](.*?)\[/i\]'is",

		"'\[u\](.*?)\[/u\]'is",
		"'\[o\](.*?)\[/o\]'is",
		"'\[s\](.*?)\[/s\]'is",

		"'\[color=(#[0-F]+|[a-z]+)\](.*?)\[/color\]'is",
		"'\[bgcolor=(#[0-F]+|[a-z]+)\](.*?)\[/bgcolor\]'is",

		"'\[center\](.*?)\[/center\]'is",
		"'\[align=(left|right|center)\](.*?)\[/align\]'is",

		"'\[ul\]'i",
		"'\[/ul\]'i",
		"'\[ol\]'i",
		"'\[/ol\]'i",
		"'\[li\](.*?)\[/li\]'is",

		"'\[indent=([0-9]+)(px|\%)\](.*?)\[/indent\]'is",

		"'\[sub\](.*?)\[/sub\]'is",
		"'\[sup\](.*?)\[/sup\]'is",

		"'\[image=(.*?)\]'i",

		"'\[br\]'i",
		
		/* new */
		"'\[quote\]'i",
		"'\[quote=(.*?)\]'i",
		"'\[/quote\]'i",
		
		"'\[code\]'i",
		"'\[/code\]'i",
		
	), 0, ($simple?7:null) );

	$replace = array_slice( array(

		"<b>\\1</b>",
		"<i>\\1</i>",

		"<u>\\1</u>",
		"<span style='text-decoration:overline;'>\\1</span>",
		"<span style='text-decoration:line-through;'>\\1</span>",

		"<span style='color:\\1;'>\\2</span>",
		"<span style='background-color:\\1;'>\\2</span>",

		"<div style='text-align:center;'>\\1</div>",
		"<div style='text-align:\\1;'>\\2</div>",

		"<ul>",
		"</ul>",
		"<ol>",
		"</ol>",
		"<li>\\1</li>",

		"<span style='margin-left:\\1\\2'>\\3</span>",

		"<sub>\\1</sub>",
		"<sup>\\1</sup>",

		"<img src='\\1' alt=''/>",

		"<br />",
		
		/* new */
		'<span class="BBQuote">',
		'<span class="BBQuote"><p class="BBQuoteOP">\\1</p>',
		'</span>',
		
		'<span class="BBCode">',
		'</span>',
		
		
	), 0, ($simple?7:null) );
	
	return preg_replace( $find, $replace, $string );

}


function GetRelativeDate( $date ){
	
	if ( is_string( $date ) ) { // SQL Date
		$date = strtotime( $date );
	}
	
	$seconds = time()-$date;
	$val = 0;
	$unit = '';
	
	if 		( $seconds < 60 )		{$unit = 'second';	$val = $seconds;}
	elseif	( $seconds < 3600 )		{$unit = 'minute';	$val = $seconds/60;}
	elseif	( $seconds < 86400 )	{$unit = 'hour';	$val = $seconds/3600;}
	elseif	( $seconds < 604800 )	{$unit = 'day';		$val = $seconds/86400;}
	elseif	( $seconds < 2419200 )	{$unit = 'week';	$val = $seconds/604800;}
	elseif	( $seconds < 31536000 )  {return date( 'M tS', $date );} // over a month ago
	else{ // over a year ago
		return date( 'M Y', $date );
	}
	
	return floor($val).' '.$unit.($val==1?'s':'').' ago';
	
}

?>